<?php

use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
use enums\HttpServerEnums;


class TestHelper
{
    private static $_instance = null;

    private function __construct()
    {
        define("ROOT_PATH", __DIR__);


        define( 'APP_ROOT', dirname( __DIR__ ) . '/' );

        define( 'APP_MODE', 'dev' );

        $this->phpSetting();

        $this->composerAutoload();

        $this->setLoader();

        $di = new FactoryDefault();


        $this->loadCfg( $di );


        Di::setDefault($di);
    }

    public static function getInstance()
    {
        if( empty( self::$_instance ) )
        {
            self::$_instance = new TestHelper();
        }

        return self::$_instance;
    }


    private function phpSetting()
    {
        error_reporting( E_ALL );

        ini_set( 'display_errors', '1' );

        //将出错信息输出到一个文本文件
        ini_set( 'error_log', APP_ROOT . '/logs/php_error_log.txt' );


        set_include_path(
            ROOT_PATH . PATH_SEPARATOR . get_include_path()
        );

    }


    private function composerAutoload()
    {
        require __DIR__ . "/../vendor/autoload.php";
    }


    private function setLoader()
    {

        $loader = new \Phalcon\Loader();
        $loader->registerNamespaces( array(
                                         'libraries' => APP_ROOT . 'libraries/',
                                         'utils'		=> APP_ROOT . 'apps/utils/',
                                         'apis'		=> APP_ROOT . 'apis',
                                         'enums'		=> APP_ROOT . 'enums/',
                                         'listeners'	=> APP_ROOT . 'listeners/',
                                         'helpers'   => APP_ROOT . 'helpers/',
                                         'tests'    => APP_ROOT . 'tests/',
                                     ));
        $loader->register();
    }


    private function loadCfg( $di )
    {
        $arrInitCfg = array(
            array( 'name' => 'lockCfg', 'file' => 'lock.php', 'inDi' => true ),
            array( 'name' => 'sessionCfg', 'file' => 'session.php', 'inDi' => true ),
            array( 'name' => 'urlCfg', 'file' => 'url.php', 'inDi' => true ),
            array( 'name' => 'dbCfg', 'file' => 'db.php', 'inDi' => true ),
            array( 'name' => 'queueCfg', 'file' => 'queue.php', 'inDi' => true ),
            array( 'name' => 'config', 'file' => 'config.php', 'inDi' => true ),
            array( 'name' => 'logCfg', 'file' => 'log.php', 'inDi' => true ),
            array( 'name' => 'cacheCfg', 'file' => 'cache.php', 'inDi' => true ),
            array( 'name' => 'emailCfg', 'file' => 'email.php', 'inDi' => true ),
        );

        //set in di
        foreach( $arrInitCfg as $cfg )
        {
            if( file_exists( APP_ROOT . 'config/' . APP_MODE . '/' . $cfg[ 'file' ] ))
            {
                if( $cfg[ 'inDi' ] )
                {
                    $di->set( $cfg['name'], require APP_ROOT . 'config/' . APP_MODE . '/' . $cfg['file'], true );
                }
                else
                {
                    ${$cfg[ 'name' ]} = require APP_ROOT . 'config/' . APP_MODE . '/' . $cfg['file'];
                }
            }
            else
            {
                if( $cfg[ 'inDi' ] )
                {
                    $di->set( $cfg['name'], require APP_ROOT . 'config/' . $cfg['file'], true );
                }
                else
                {
                    ${$cfg[ 'name' ]} = require APP_ROOT . 'config/' . $cfg['file'];
                }
            }
        }


        @ini_set( 'session.name', HttpServerEnums::SESSION_KEY );

        require APP_ROOT . 'config/services.php';//加载公共services

    }


}

TestHelper::getInstance();